include("formula.jl")
formula = loadformula();

function parseformula(name, num)
    if length(name) != length(num)
        println("ERROR")
        return "ERROR"
    end
    if typeof(num[1]) != Rational{Int32}
        str = "\\text{手搓}"
    else
        str = ""
        for i=1:length(name)
            na = name[i]
            b = numerator(num[i])
            a = denominator(num[i])
            if a!=1
                p="\\frac{$b}{$a}"
            elseif b==1
                p = ""
            else
                p = "$b"
            end
            str = str*"+$p\\text{$na}"
        end
        str = str[2:end]
    end
    return str
end

f = open("formulatex.txt","w")

for i=1:length(formula)
    println(formula[i].product)
    to=parseformula(formula[i].product, formula[i].productNum)
    from=parseformula(formula[i].material, formula[i].materialNum)
    req = formula[i].cost
    if typeof(req)==Float64
        req = ",t=$req s"
    elseif !isempty(req)
        req = ","*req
    end
    form="\$$from \\rightarrow $to\$$req\\\\\n"
    print(form)
    print(f,form)
end

close(f)
